package com.arvin.service.serviceImpl;
import com.arvin.mapper.*;
import com.arvin.pojo.*;
import com.arvin.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static com.arvin.utils.Md5Util.getMD5String;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private InterviewMapper interviewMapper;
    @Autowired
    private ExemptMapper exemptMapper;
    @Autowired
    private ClassMapper classMapper;

    /**
     * 根据用户名查找管理员
     * @param username
     * @return
     */
    public Admin findByUsername(String username) {
        Admin admin = adminMapper.findByUsername(username);
        return admin;
    }

    /**
     * 添加管理员
     * @param admin
     */
    public void addAdmin(Admin admin) {
        //密码MD5加密
        String md5String = getMD5String(admin.getPassword());
        admin.setPassword(md5String);
        adminMapper.addAdmin(admin);
    }

    public void updateBanState(String username, Integer ban) {
        adminMapper.updateBanState(username,ban);
    }

    /**
     * 修改管理员密码
     * @param newPassword
     * @param username
     */
    public void updatePassword(String newPassword, String username) {
        String md5String = getMD5String(newPassword);
        adminMapper.updatePassword(md5String,username);
    }

    /**
     * 获取入班人员
     * @param page
     * @param pageSize
     * @param studentId
     * @param stuName
     * @return
     */
    public PageBean getEnterClassInfo(Integer page, Integer pageSize, String studentId, String stuName,String className) {
        PageHelper.startPage(page,pageSize);
        List<IntvInfo> intvInfoList = adminMapper.getEnterClassInfo(studentId,stuName,className);
        for (IntvInfo intvInfo: intvInfoList){
            List<Contest> list = studentMapper.findByStudentIdToContest(intvInfo.getStudentId());
            intvInfo.setContest(list);
        }
        Page<IntvInfo> intvInfoPage = (Page<IntvInfo>) intvInfoList;
        PageBean pageBean = new PageBean(intvInfoPage.getTotal(),intvInfoPage.getResult());
        return pageBean;
    }

    /**
     * 退班
     * @param studentId
     */
    @Transactional
    public void deleteStudent(String studentId) {
        Student student = studentMapper.findByStudentId(studentId);
        examMapper.deleteExam(student.getExamId());
        interviewMapper.deleteIntv(student.getIntvId());
        exemptMapper.deleteExem(student.getExemId());
        adminMapper.deleteStudent(studentId);
        classMapper.deleteByStudentId(studentId);
    }

    /**
     * 获取管理员信息列表
     * @param username
     * @return
     */
    @Override
    public List<Admin> getAdminInfoList(String username) {
        return adminMapper.getAdminInfoList(username);
    }

    /**
     * 更新管理员信息
     * @param admin
     */
    @Override
    public void updateAdminInfo(Admin admin) {
        if (StringUtils.hasLength(admin.getPassword())){
            String md5String = getMD5String(admin.getPassword());
            admin.setPassword(md5String);
        }else {
            admin.setPassword(null);
        }
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.updateAdminInfo(admin);
    }

    /**
     * 删除管理员信息
     * @param username
     */
    @Override
    public void deleteAdminInfo(String username) {
        adminMapper.deleteAdminInfo(username);
    }
}
